<?php

/**
 * 网银在线支付接口
 */
class AlipayPay extends Pay {

    public $config = array();

    public function __construct($config = array()) {
        $this->config = $config;

        if ($this->config['alipay_pay_type'] == 1)
            $this->config['service'] = 'create_partner_trade_by_buyer'; //担保
        elseif ($this->config['alipay_pay_type'] == 3)
            $this->config['service'] = 'create_direct_pay_by_user'; //即时
        else
            $this->config['service'] = 'trade_create_by_buyer'; //标准

        $this->config['gateway_url'] = 'https://www.alipay.com/cooperate/gateway.do?';
        $this->config['gateway_method'] = 'POST';
        $this->config['notify_url'] = $this->config['return_url_hide'];
        $this->config['return_url'] = $this->config['return_url_show'];
    }

    public function setup() {
        $modules = array();
        $modules['pay_name'] = '支付宝';
        $modules['pay_code'] = 'Alipay';
        $modules['pay_desc'] = '支付宝';
        $modules['is_cod'] = '0';
        $modules['is_online'] = '1';
        $modules['author'] = 'Mr.Wang';
        $modules['website'] = 'http://www.alipay.com';
        $modules['version'] = '1.0.0';
        $modules['config'] = array(
        );
        return $modules;
    }

    /**
     * 生成订单
     * @return string 
     */
    public function pay_get(&$params) {
        $parameter = array(
            'service' => $this->config['service'],
            'partner' => trim($this->config['alipay_partner']),
            '_input_charset' => 'utf-8',
            'notify_url' => trim($this->config['notify_url']),
            'return_url' => trim($this->config['return_url']),
            /* 商品信息 */
            'subject' => $this->config['pay_sn'],
            'out_trade_no' => $this->config['pay_sn'],
            'price' => format_price($this->config['pay_price']),
            'body' => $this->config['pay_desc'],
            'quantity' => 1,
            'payment_type' => 1,
            /* 物流参数 */
            'logistics_type' => 'EXPRESS',
            'logistics_fee' => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
            //'agent'             => $this->config['agent'], 

            /* 买卖双方信息 */
            'seller_email' => trim($this->config['alipay_account'])
        );
        ksort($parameter);
        reset($parameter);
        $param = '';
        $sign = '';

        foreach ($parameter AS $key => $val) {
            $param .= "$key=" . urlencode($val) . "&";
            $sign .= "$key=$val&";
        }

        $param = substr($param, 0, -1);
        $sign = substr($sign, 0, -1) . $this->config['alipay_key'];
        //$sign  = substr($sign, 0, -1). ALIPAY_AUTH;
        $parameter['sign'] = MD5($sign);
        $parameter['sign_type'] = "MD5";
        //$button = '<span><input type="button"  class="button" onclick="window.open(\'' . $this->config['gateway_url'] . $param . '&sign=' . MD5($sign) . '&sign_type=MD5\')" value="' . L('PAY_NOW') . '" /></span>';
        $button = $this->buildRequestForm($this->config['gateway_url'], "get", $parameter, L('PAY_NOW'));
        return $button;
    }

    /**
     * 确认订单
     * @return boolean 
     */
    public function pay_confirm(&$params) {
        $pay_id = $_GET['pay_id'];
        unset($_GET['_URL_']);
        unset($_GET['pay_id']);
        if (!empty($_POST)) {
            foreach ($_POST as $key => $data) {
                $_GET[$key] = $data;
            }
        }
        $order_sn = trim($_GET['out_trade_no']);
        /* 检查数字签名是否正确 */
        ksort($_GET);
        reset($_GET);
        $sign = '';
        foreach ($_GET AS $key => $val) {
            if ($key != 'sign' && $key != 'sign_type' && $key != 'code' && $key != 'g' && $key != 'm' && $key != 'a') {
                $sign .= "$key=$val&";
            }
        }
        $sign = substr($sign, 0, -1) . $this->config['alipay_key'];
        if (md5($sign) != $_GET['sign']) {
            return reStatus(false, "出现错误", "fail");
        }
        $param = array(
            "pay_id" => $pay_id,
            "pay_sn" => $order_sn,
            "price" => format_int(intval($_GET['price'] * 100)),
        );
        if ($_GET['trade_status'] == 'WAIT_SELLER_SEND_GOODS' || $_GET['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS' || $_GET['trade_status'] == 'WAIT_BUYER_PAY') {
            /* 改变订单状态 进行中 */
            //order_pay_status($order_sn, '1');
            return reStatus(true, "付款成功,请联系商家确认收货后,完成支付.", "success");
        } elseif ($_GET['trade_status'] == 'TRADE_FINISHED') {
            /* 改变订单状态 */
            service("Pay")->payPrice($param);
            return reStatus(true, "支付成功", "success");
        } elseif ($_GET['trade_status'] == 'TRADE_SUCCESS') {
            /* 改变订单状态 即时交易成功 */
            service("Pay")->payPrice($param);
            return reStatus(true, "支付成功", "success");
        } else {
            return reStatus(false, "出现错误", "fail");
        }
    }

}

?>